package ${scfg._dao_pkg};

import com.tais.framework.dao.IBaseDao;
import com.tais.framework.entity.IEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>${tcfg.tableComment} 数据库处理接口类。
 */
@Mapper
public interface I${tcfg.tableNameFormat!}${scfg._dao_suffix!} extends IBaseDao {

    /**
      * 判断是否存在
      */
    @Select("select IFNULL(count(0),0) as count from ${tcfg.tableName} where @[ for(t in pks){ /]${tLP.index==1?'':' and '} ${t.columnName} = #{${t.columnNameFormat}}@[ } /] ")
    int isDataHasExist(IEntity entity) throws Exception;

    @[ if ("1"==scfg._delLogic && null!=scfg._col_del) { /]
    /**
     * 逻辑删除
     */
    @Update("update ${tcfg.tableName} set ${null!=scfg._col_version?scfg._col_version+'='+scfg._col_version+'+1,':''} ${null!=scfg._col_updated?scfg._col_updated+'=now(),':''} ${scfg._col_del}=1 where @[ for(t in pks){ /]${tLP.index==1?'':' and '} ${t.columnName} = #{${t.columnNameFormat}}@[ } /] ")
    int deleteById(IEntity entity) throws Exception;
    @[ } /]

    @[ if ("1"==scfg._recovery && null!=scfg._col_del) { /]
    /**
     * 恢复逻辑删除的数据
     */
    @Update("update ${tcfg.tableName} set  ${null!=scfg._col_version?scfg._col_version+'='+scfg._col_version+'+1,':''} ${null!=scfg._col_updated?scfg._col_updated+'=now(),':''} ${scfg._col_del}=0 where @[ for(t in pks){ /]${tLP.index==1?'':' and '} ${t.columnName} = #{${t.columnNameFormat}}@[ } /] ")
    int recoveryDataById(IEntity entity) throws Exception;
    @[ } /]

    @[ if ("1"==scfg._del) { /]
    /**
     * 根据主键 物理删除
     */
    @Delete("delete from ${tcfg.tableName} where @[ for(t in pks){ /]${tLP.index==1?'':' and '} ${t.columnName} = #{${t.columnNameFormat}}@[ } /] ")
    int deleteByPrimaryKey(IEntity entity) throws Exception;

    @[ if (pks.~size>1) {
    for(t in pks){
    /]
    /**
      * 根据主键${t.columnName} 物理删除
      */
    @Delete("delete from ${tcfg.tableName} where  ${t.columnName} = #{${t.columnNameFormat}}")
    int deleteBulkDataBy${strUtil.toUpperCaseFirstOne(t.columnNameFormat)}(IEntity entity) throws Exception;
    @[ }
    } /]
    @[ } /]
}
